package com.cj.wtrails.utils;

import android.app.Activity;
import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.cj.wtrails.R;

/**
 * 图片加载
 * auth zengyi
 * created at 2017/11/4 17:00
 */

public class GlideUtils {

	private static RequestOptions options = new RequestOptions()
			.placeholder(R.drawable.img_default)
			.error(R.drawable.img_default)
			.centerCrop()
			.diskCacheStrategy(DiskCacheStrategy.ALL);

	private static RequestOptions options4 = new RequestOptions()
			.placeholder(R.drawable.img_default)
			.error(R.drawable.img_default)
			.fitCenter()
			.diskCacheStrategy(DiskCacheStrategy.ALL);
	private static RequestOptions mOptions =
			RequestOptions.bitmapTransform(new RoundedCorners(8));


	public static void setImageCenter(Context context, String imgPath, ImageView imageView) {
		//防止内存泄漏导致崩溃
		if (context instanceof Activity) {
			if (((Activity) context).isFinishing()) return;
		}
		Glide.with(context).load(imgPath).apply(options).into(imageView);
	}

	public static void setImageCenter(Context context, int res, ImageView imageView) {
		if (context instanceof Activity) {
			if (((Activity) context).isFinishing()) return;
		}
		Glide.with(context).load(res).apply(options).into(imageView);
	}

	public static void setImageCenterInside(Context context, String imgPath, ImageView imageView) {
		if (context instanceof Activity) {
			if (((Activity) context).isFinishing()) return;
		}
		Glide.with(context).load(imgPath).apply(options4).into(imageView);
	}

	public static void imageloadCircleCrop8(Context context, String imgPath, ImageView imageView) {
		if (context instanceof Activity) {
			if (((Activity) context).isFinishing()) return;
		}
		Glide.with(context).load(imgPath).apply(mOptions).into(imageView);
	}

	public static void imageloadCircleCrop8(Context context, int res, ImageView imageView) {
		if (context instanceof Activity) {
			if (((Activity) context).isFinishing()) return;
		}
		Glide.with(context).load(res).apply(mOptions).into(imageView);
	}

}